package com.shengdong.water.code.viewmodel

import com.shengdong.water.base.BaseViewModel
import com.shengdong.water.code.bean.NotifyMsg
import com.shengdong.water.code.bean.PageData
import com.shengdong.water.code.repository.ApiRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

@HiltViewModel
class WorkViewModel @Inject constructor(
    private val repository: ApiRepository
) : BaseViewModel() {

    private val _notifyMsg = MutableStateFlow<PageData<NotifyMsg>?>(null)
    val notifyMsg: StateFlow<PageData<NotifyMsg>?> = _notifyMsg

    fun getNotify() {
        handleRequest(
            flow = repository.getNotify(current = 1, size = 4),
            onSuccess = { _notifyMsg.value = it },
            onError = { code ,msg ->
                when (code) {
                    401 ->  {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError("获取用户信息失败: [$code] $msg")
                }

            },
            onToast =  {
                it
            },
            onException = {e ->
                showError("发生错误: ${e.message}")
            },
        )
    }
}
